﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.SqlClient;

public partial class product_saving_user_transfers : System.Web.UI.Page
{
    

    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "TRANSFER";

         //String saving_acc= Session["savingAccountNo"] as String;
     
          //this.Label2.Text =saving_acc;
        SqlConnection conn = DBManager.getSqlConnection();

        string sql = "select accountNo from  SAVING_ACCOUNT where custID = '" + Session["userId"] + "'";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Connection.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        String saving_acc = this.Label2.Text;

        if (dr.Read())
        {
            Label2.Text = dr.GetString(0); // assign to your label

        }

        dr.Close();
        cmd.Connection.Close();

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal amount = decimal.Parse(tbAmount.Text);
        string strType = "FUND TRANSFER";
        //string strFrom = Label2.Text;
        string strTo = tbTo.Text;
        string strStatus = "SUCCESS";


        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        //int maxValue = 0; 
        SqlConnection myConnection = new SqlConnection(strConnectionString);
        //string strCommandText = "SELECT MAX(transactionID) AS lastID FROM SUMMARY;";
        string strCommandText = "";
        //SqlCommand myCommand = new SqlCommand(strCommandText, myConnection);
        //myConnection.Open();
        //SqlDataReader myReader = myCommand.ExecuteReader();
        ////while (myReader.Read())
        ////maxValue = int.Parse(myReader["lastID"].ToString());
        //myReader.Close();
        //myConnection.Close();

        //strCommandText = "update [SAVING_ACCOUNT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + tbTo.Text;
        //strCommandText = "update [SAVING_ACCOUNT] SET [balance] = balance-" + amount + " WHERE [accountNo] = " + this.Label2.Text;
        //strCommandText = "INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, credit)" + "VALUES(@date, @type, @status, @accNo, @credit)";
        //strCommandText = "INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, debit)" + "VALUES(@date, @type, @status, @accNo, @debit)";

        try
        {
        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
        myConnection.Open();
        myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
        myCommandd.Parameters.AddWithValue("@type", strType);
        myCommandd.Parameters.AddWithValue("@status", strStatus);
        myCommandd.Parameters.AddWithValue("@accNo", tbTo.Text);
        myCommandd.Parameters.AddWithValue("@transactionAmount", amount);
        myCommandd.ExecuteNonQuery();

        
        SqlCommand myCommandw = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo, transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
        myCommandw.Parameters.AddWithValue("@date", DateTime.Now);
        myCommandw.Parameters.AddWithValue("@type", strType);
        myCommandw.Parameters.AddWithValue("@status", strStatus);
        myCommandw.Parameters.AddWithValue("@accNo", this.Label2.Text);
        myCommandw.Parameters.AddWithValue("@transactionAmount", 0-amount);
        myCommandw.ExecuteNonQuery();

        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandp = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + tbTo.Text, myConnection);
        myConnection.Open();
        myCommandp.Parameters.AddWithValue("@balance", amount);
        myCommandp.Parameters.AddWithValue("@account", tbTo.Text);
        myCommandp.ExecuteNonQuery();
        
        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandm = new SqlCommand(strCommandText = "update [SAVING_ACCOUNT] SET [balance] = balance-" + amount + " WHERE [accountNo] = " + this.Label2.Text, myConnection);
        myConnection.Open();
        myCommandm.Parameters.AddWithValue("@balance", amount);
        myCommandm.Parameters.AddWithValue("@account", Label2.Text);
        myCommandm.ExecuteNonQuery();

        Label1.Text = "Fund Transfer Success, please log in to check for the updated balance. Thank you.";

        myConnection.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
        Response.Write(ex);
        }
        finally
        {
        myConnection.Close();
        }
        
    }
}